/* 
 * File:   main.c
 * Author: abadalya
 *
 * Created on November 26, 2011, 8:33 PM
 */

#include <stdio.h>
#include <stdlib.h>

#include "program.h"

#include "parser.tab.h"

extern FILE* yyin;
extern int yyparse();

struct program* module = 0;

/**
 * Running SFL0.
 * 
 * ~ sflo file.fu
 * 
 */
int main(int argc, char** argv)
{
    static const char* banner = "This is Semi-Functional Language 0.\n"
                "Created by Armen Badalian, 2011.";
    static const char* usage = "Usage:\n  sflo <filename>";

    /* banner and usage */
    if(argc == 1) {    
        printf("%s\n", banner);
        return 0;
    }
    
    /* incorrect command line */
    if(argc > 2) {
        printf("Invalid command line.\n%s\n", usage);
        return 1;
    }
    
    /* normal start */
    yyin = fopen(argv[1], "rb");
    if(NULL == yyin) {
        printf("Cannot work with input file.\nMaybe it does not exists or corrupted.\n");
        return 2;
    }

    module = create_program();
    
    int res = yyparse();
    if( res == 0 )
        res = execute_program(module);
    return res;
}

